package com.intpro.demo.Controller;

import com.intpro.demo.DAO.CommonRep;
import com.intpro.demo.DAO.User;
import com.intpro.demo.Service.SearchHistoryService;
import com.intpro.demo.Utils.CodeException;
import com.intpro.demo.Utils.MyAnnotations.Authorization;
import com.intpro.demo.Utils.MyAnnotations.CurrentUser;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@RequestMapping("api/searchhis")
public class SeachHisController
{

	@Autowired
	private SearchHistoryService searchHistoryService;

	@Authorization
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public CommonRep add(@CurrentUser User user, @RequestBody Map<String, String> req)
			throws CodeException
	{
		String c = req.get("content");
		if (c == null)
		{
			throw CodeException.NotNullErr();
		}
		searchHistoryService.add(user, c);
		return new CommonRep();
	}

	@Authorization
	@RequestMapping(value = "find", method = RequestMethod.POST)
	public CommonRep find(@CurrentUser User user) throws CodeException
	{
		CommonRep rep = new CommonRep();
		rep.data = searchHistoryService.find(user);
		return rep;
	}
}
